<?php

namespace App\Http\Controllers\Backend\UserManage;

use App\Http\Controllers\Controller;
use App\Http\Repositories\Relation\UserRoleRepo;
use App\Http\Repositories\UserManage\RoleRepo;
use App\Http\Requests\Validators\Backend\UserManage\RoleValidator;
use App\Traits\HelpFunctions;
use Illuminate\Http\Request;

/**
 * 角色相关
 */
class RoleController extends Controller
{
    use HelpFunctions;

    const VALIDATOR = RoleValidator::class;

    public function __construct()
    {
        $this->self_repo = new RoleRepo;
    }

    public function addRoleToUser(Request $request)
    {
        //参数验证
        $args = $request->only(
            'user_id',
            'role_id'
        );

        $this->my_validate($args, 'addRoleToUserRules');

        return app(UserRoleRepo::class)->create($args['user_id'], $args['role_id']);
    }

    public function search(Request $request)
    {
        //参数验证
        $args = $request->only(
            'name',
            'alias',
            'begin_time',
            'end_time'
        );

        $this->my_validate($args, 'searchRules');

        return $this->self_repo->search($args);
    }

    public function store(Request $request)
    {
        //参数验证
        $args = $request->only(
            'name',
            'alias',
        );

        $this->my_validate($args, 'storeRules');

        //添加数据
        return $this->returnResponse($this->self_repo->createOne($args));
    }

    public function delete($id)
    {
        return $this->self_repo->deleteById($id);
    }

    public function edit(Request $request, $id)
    {
        //参数验证
        $args = $request->only(
            'name',
            'alias',
        );
        $this->my_validate($args, 'editRules');

        //根据id修改数据
        return $this->returnResponse($this->self_repo->editById($id, $args));
    }
}
